﻿using Business.BorrowManagement;
using Business.BorrowManagement.Dto;
using Business.CheckQManagement.Dto;
using Business.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Domain.Repositories;
using XCZ;
using Yitter.IdGenerator;

namespace Business.CheckQManagement
{
    public class CheckQAppService : BusinessWorkFlowAppService, ICheckQAppService
    {
        private const string FormName = "CheckQ";
        private IRepository<CheckQ, Guid> _repository;
        private IRepository<CheckDetail, Guid> _repository1;




        public CheckQAppService(
            IRepository<CheckQ, Guid> repository, IRepository<CheckDetail, Guid> repository1
            )
        {
            _repository = repository;
            _repository1 = repository1;
        }
        public async Task<CheckQDto> Get(Guid id)
        {
            var query = (from b in await _repository.GetQueryableAsync()
                         where id == b.Id
                         // on b.TenantId equals d.BorrowId
                         select new CheckQDto
                         {
                             Id = b.Id,
                             CCom = b.CCom,
                             CName = b.CName,
                             CreatePeo = b.CreatePeo,
                             CTime = b.CTime,
                             CConP = b.CConP,
                             CDesc = b.CDesc,
                             createOrUpdateCheckQDto = (from d in _repository1.GetQueryableAsync().Result
                                              where b.Id == d.CheckQId
                                              select new CreateOrUpdateCheckQDto
                                              {
                                                  CDTitle = d.CDTitle,
                                                  CDNum = d.CDNum,
                                                  CDPeo = d.CDPeo,
                                                  CDPName = d.CDPName,
                                                  CDPNum = d.CDPNum,
                                                  CDSNum = d.CDSNum,
                                                  CDUnit = d.CDUnit,
                                                  CDPrice = d.CDPrice,
                                                  CDANum = d.CDANum,
                                                  CDQuesNum = d.CDQuesNum,
                                                  CDShopNum = d.CDShopNum,
                                                  CDPH = d.CDPH,
                                                  CDBTime = d.CDBTime,
                                                  CDTime = d.CDTime,
                                                  CDATime = d.CDATime,
                                                  CDPage = d.CDPage,
                                                  CDDesc = d.CDDesc
                                              }
                             ).ToList()
                         }
                   ).ToList();



            //var data = await _repository.GetAsync(id);
            //var dto = ObjectMapper.Map<BorrowM, BorrowDto>(data);
            return query.ToList()[0];
        }

        public async Task<PagedResultDto<CheckQDto>> GetAll(GetCheckQInputDto input)
        {
            //var query = (await _repository.GetQueryableAsync()).WhereIf(!string.IsNullOrWhiteSpace(input.Filter), a => a.BTitle.Contains(input.Filter));

            var query = (from b in await _repository.GetQueryableAsync()
                         select new CheckQDto
                         {
                             Id = b.Id,
                             CCom = b.CCom,
                             CName = b.CName,
                             CreatePeo = b.CreatePeo,
                             CTime = b.CTime,
                             CConP = b.CConP,
                             CDesc = b.CDesc,
                             createOrUpdateCheckQDto = (from d in _repository1.GetQueryableAsync().Result
                                                        where b.Id == d.CheckQId
                                                        select new CreateOrUpdateCheckQDto
                                                        {
                                                            CDTitle = d.CDTitle,
                                                            CDNum = d.CDNum,
                                                            CDPeo = d.CDPeo,
                                                            CDPName = d.CDPName,
                                                            CDPNum = d.CDPNum,
                                                            CDSNum = d.CDSNum,
                                                            CDUnit = d.CDUnit,
                                                            CDPrice = d.CDPrice,
                                                            CDANum = d.CDANum,
                                                            CDQuesNum = d.CDQuesNum,
                                                            CDShopNum = d.CDShopNum,
                                                            CDPH = d.CDPH,
                                                            CDBTime = d.CDBTime,
                                                            CDTime = d.CDTime,
                                                            CDATime = d.CDATime,
                                                            CDPage = d.CDPage,
                                                            CDDesc = d.CDDesc
                                                        }
                             ).ToList()
                         }
                  ).ToList();



            var totalCount = query.Count();
            var items = query.Skip(input.SkipCount - 1).Take(input.MaxResultCount).ToList();

            //var dtos = ObjectMapper.Map<List<BorrowM>, List<BorrowDto>>(items);
            return new PagedResultDto<CheckQDto>(totalCount, query);
        }

        public async Task<CheckQDto> CreateOrUpdate(CheckQDto input)
        {
            CheckQ result = null;
            if (!input.Id.HasValue)
            {
                input.Id = GuidGenerator.Create();
                input.CTime=DateTime.Now;
                 CheckQ borrow = new CheckQ()
                {
                   
                    CCom = input.CCom,
                    CName = input.CName,
                    CreatePeo = input.CreatePeo,
                    CTime = input.CTime,
                    CConP = input.CConP,
                    CDesc = input.CDesc,

                };
                foreach (var item in input.createOrUpdateCheckQDto)
                {
                    CheckDetail detail = new CheckDetail()
                    {
                        CheckQId = input.Id,
                        CDTitle = item.CDTitle,
                        CDNum = item.CDNum,
                        CDPeo = item.CDPeo,
                        CDPName = item.CDPName,
                        CDPNum = item.CDPNum,
                        CDSNum = item.CDSNum,
                        CDUnit = item.CDUnit,
                        CDPrice = item.CDPrice,
                        CDANum = item.CDANum,
                        CDQuesNum = item.CDQuesNum,
                        CDShopNum = item.CDShopNum,
                        CDPH = item.CDPH,
                        CDBTime = item.CDBTime,
                        CDTime = item.CDTime,
                        CDATime = item.CDATime,
                        CDPage = item.CDPage,
                        CDDesc = item.CDDesc
                    };
                    await _repository1.InsertAsync(detail);

                };
                result = await _repository.InsertAsync(ObjectMapper.Map<CheckQDto, CheckQ>(input));

                //创建工作流
                // await WorkFlow.CreateWorkFlow(FormName, result);
            }
            else
            {
                var data = await _repository.GetAsync(input.Id.Value);

                //BorrowM borrow = new BorrowM()
                //{
                //    BTitle = input.BTitle,
                //    BNum = input.BNum,
                //    BPeo = input.BPeo,
                //    BTme = input.BTme,
                //    BHand = input.BHand,
                //    BCon = input.BCon

                //};
                //borrow=await _repository.UpdateAsync(borrow);
                //foreach (var item in input.borrowDetails)
                //{
                //    BorrowPro detail = new BorrowPro()
                //    {
                //        BorrowId =(Guid) input.Id,
                //        PName = item.PName,
                //        PNum = item.PNum,
                //        SNum = item.SNum,
                //        BUnit = item.BUnit,
                //        BorNum = item.BorNum,
                //        GoBit = item.GoBit,
                //        BitInfo = item.BitInfo,
                //        GiveTime = item.GiveTime,
                //        PageStyle = item.PageStyle,
                //        Desc = item.Desc
                //    };
                //    await _repository1.UpdateAsync(detail);

                //};


                result = await _repository.UpdateAsync(ObjectMapper.Map(input, data));
                //检查流程状态
                //await WorkFlow.CheckFormStatus(input.Id.Value, result);
            }
            return ObjectMapper.Map<CheckQ, CheckQDto>(result);
        }

        public async Task Delete(List<Guid> ids)
        {
            foreach (var item in ids)
            {
                await _repository.DeleteAsync(item);
            }

        }
    }
}
